﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Model;
using System.Configuration;
using Ext.Net;

namespace TCDC_System.UserControl
{
    public partial class CampaignManageAdd : System.Web.UI.UserControl
    {

        private tcdc_mst_campaign objCampaign;
        private tcdc_mst_member_type objMemberType;

        protected void Page_Load(object sender, EventArgs e)
        {
            objCampaign = new tcdc_mst_campaign();
            objCampaign.strConn = ConfigurationManager.AppSettings["ConnString"];
            objMemberType = new tcdc_mst_member_type();
            objMemberType.strConn = ConfigurationManager.AppSettings["ConnString"];

            /*** ComBobox Campaign Group ***/
            StoreCampignGroup.DataSource = GetCampaignGroup();
            StoreCampignGroup.DataBind();

            SetGridMemberType();
            
        }

        public void Show()
        {
            WindowCampaignManageAdd.Show();
        }

        public Store GridStore { get; set; } 

        protected void SetGridMemberType()
        {
            tcdc_mst_member_type objMemberType = new tcdc_mst_member_type();
            objMemberType.strConn = ConfigurationManager.AppSettings["ConnString"];
            DataTable dtMemberType = objMemberType.getByObject(new tcdc_mst_member_type());

            DataTable dt = new DataTable();
            DataRow row;
            dt.Columns.Add("member_type_id");
            dt.Columns.Add("member_type_name_th");
            dt.Columns.Add("member_type_name_en");
            dt.Columns.Add("add_year");
            dt.Columns.Add("add_month");
            dt.Columns.Add("price");

            foreach (DataRow r in dtMemberType.Rows)
            {
                row = dt.NewRow();
                row["member_type_id"] = r["member_type_id"];
                row["member_type_name_th"] = r["member_type_name_th"];
                row["member_type_name_en"] = r["member_type_name_en"];
                row["add_year"] = "";
                row["add_month"] = "";
                row["price"] = "";

                dt.Rows.Add(row);
            }

            StoreMemberType.DataSource = dt;
            StoreMemberType.DataBind();
        }

        protected DataTable GetCampaignGroup()
        {
            tcdc_mst_campaign_group objCampaingG = new tcdc_mst_campaign_group();
            objCampaingG.strConn = ConfigurationManager.AppSettings["ConnString"];
            DataTable dt = objCampaingG.getByObject(new tcdc_mst_campaign_group());

            return dt;
        }

        protected void SaveCampaign(object sender, DirectEventArgs e)
        {
            tcdc_mst_campaign objCampaign = new tcdc_mst_campaign();
            objCampaign.strConn = ConfigurationManager.AppSettings["ConnString"];

            string json = e.ExtraParams["MemberTypeValue"];

            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);

            foreach (Dictionary<string, string> row in workingdays)
            {
                tcdc_mst_campaign c = new tcdc_mst_campaign();
                c.product_id = 1;
  
                c.campaign_group_id = Convert.ToInt32(combo_campaign_group.Text);
                
                c.member_type_id = Convert.ToInt32(row["member_type_id"]);
                c.campaign_name_th = txt_campaign_name_th.Text;


                if (row["add_year"].Equals(""))
                {
                    c.add_year = 0;
                }
                else
                {
                    c.add_year = Convert.ToInt32(row["add_year"]);             
                }

                if (row["add_month"].Equals(""))
                {
                    c.add_month = 0;
                }
                else
                {
                    c.add_month = Convert.ToInt32(row["add_month"]);
                }

                if (row["price"].Equals(""))
                {
                    c.price = 0.00;
                }
                else
                {
                    c.price = Convert.ToDouble(row["price"]);
                }
                
                c.begin_date = txt_begin_person.Text;
                c.end_date = txt_end_person.Text;
                c.promotion_code = "1";
                c.promotion_public = "Y";
                c.site_id = 1;


                objCampaign.insertData(c);
            }

            GridStore.DataSource = GetCampaign();
            GridStore.DataBind();

            WindowCampaignManageAdd.Hide();

        }

        protected DataTable GetCampaign()
        {

            DataTable dtCampaign = objCampaign.getByObject(new tcdc_mst_campaign());
            DataTable dtMemberType = objMemberType.getByObject(new tcdc_mst_member_type());
            DataTable dt = new DataTable();
            DataRow row;

            dt.Columns.Add("campaign_id");
            dt.Columns.Add("campaign_name");
            dt.Columns.Add("begin_date", typeof(DateTime));
            dt.Columns.Add("end_date", typeof(DateTime));
            dt.Columns.Add("member_type_id");
            dt.Columns.Add("member_type_name_en");
            dt.Columns.Add("price");

            var result = from table1 in dtCampaign.AsEnumerable()
                         join table2 in dtMemberType.AsEnumerable() on table1["member_type_id"] equals table2["member_type_id"]
                         select new
                         {
                             campaign_id = table1["campaign_id"],
                             campaign_name = table1["campaign_name"],
                             begin_date = table1["begin_date"],
                             end_date = table1["end_date"],
                             member_type_id = table1["member_type_id"],
                             member_type_name_en = table2["member_type_name_en"],
                             price = table1["price"]
                         };

            foreach (var item in result)
            {
                row = dt.NewRow();
                row["campaign_id"] = item.campaign_id;
                row["campaign_name"] = item.campaign_name;
                row["begin_date"] = (DateTime)item.begin_date;
                row["end_date"] = (DateTime)item.end_date;
                row["member_type_id"] = item.member_type_id;
                row["member_type_name_en"] = item.member_type_name_en;
                row["price"] = item.price;
                dt.Rows.Add(row);
            }

            dt.DefaultView.Sort = "campaign_id , member_type_id";

            return dt;
        }

        protected void MessageWarning(String message)
        {
            X.Msg.Show(new MessageBoxConfig
            {
                Title = "Warning Message",
                Message = message,
                Buttons = MessageBox.Button.OK,
                Icon = (MessageBox.Icon)Enum.Parse(typeof(MessageBox.Icon), "WARNING"),
            });
        }
    }
}